package web;

import pojo.User;
import service.UserService02;
import service.impl.UserServiceImpl02;
import utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class UserServlet02 extends BaseServlet02 {
    private UserService02 userService02 = new UserServiceImpl02();

    protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user = WebUtils.copyParamToBean(req.getParameterMap(), new User());
        User login = userService02.login(user);
        if (login == null) {
            System.out.println("登录失败");
            req.setAttribute("msg", "用户名或密码错误！");
            req.setAttribute("username", user.getUsername());
            req.getRequestDispatcher("/pages/user/login.jsp").forward(req, resp);
        } else {
            System.out.println("登陆成功！");
            req.getRequestDispatcher("/pages/user/login_success.jsp").forward(req, resp);
        }
    }


    protected void regist(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user = WebUtils.copyParamToBean(req.getParameterMap(), new User());
        String code = req.getParameter("code");
        if ("abcde".equalsIgnoreCase(code)) {
            if (userService02.existsUser(user.getUsername())) {
                System.out.println("用户名" + user.getUsername() + "已存在！");
                req.setAttribute("msg", "用户名已存在！");
                req.setAttribute("username", user.getUsername());
                req.setAttribute("email", user.getEmail());
                req.getRequestDispatcher("pages/user/regist_bind(click_blur)事件.jsp").forward(req,resp);
            } else {
                userService02.regist(user);
                req.getRequestDispatcher("pages/user/regist_success.jsp").forward(req,resp);
            }
        } else {
            System.out.println("验证码" + code + "错误！");
            req.setAttribute("msg", "验证码错误！");
            req.setAttribute("username", user.getUsername());
            req.setAttribute("email", user.getEmail());
            req.getRequestDispatcher("pages/user/regist_bind(click_blur)事件.jsp").forward(req,resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
